﻿Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.DAO
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General
Imports DevComponents.DotNetBar

Public Class frmKhachHang
    Private m_KhachHangCtrl As New KhachHangCtrl()
    Private m_QuyDinh As New QuyDinh()
    Public Sub New()
        InitializeComponent()
        DataService.OpenConnection()
    End Sub

    Private Sub frmKhachHang_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        m_KhachHangCtrl.HienThi(DGVKhachHang, BindingNavigatorKhachHang, txtMaKH, txtHoTen, txtDiaChi, txtDienThoai)
    End Sub

    Private Sub bindingNavigatorRefreshItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorRefreshItem.Click
        txtTimKiem.Text = ""
        m_KhachHangCtrl.HienThi(DGVKhachHang, BindingNavigatorKhachHang, txtMaKH, txtHoTen, txtDiaChi, txtDienThoai)
    End Sub
    Public Function KiemTraTruocKhiLuu(str As [String]) As [Boolean]
        For Each row As DataGridViewRow In DGVKhachHang.Rows
            If row.Cells(str).Value IsNot Nothing Then
                Dim m_Str As [String] = row.Cells(str).Value.ToString()
                If m_Str = "" Then
                    Return False
                End If
            End If
        Next
        Return True
    End Function

    Private Sub bindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorSaveItem.Click
        If KiemTraTruocKhiLuu("colMaKH") = True AndAlso KiemTraTruocKhiLuu("colDiaChi") = True AndAlso KiemTraTruocKhiLuu("colHoTen") = True Then
            bindingNavigatorPositionItem.Focus()
            m_KhachHangCtrl.LuuKhachHang()
        Else
            MessageBoxEx.Show("Giá trị ô không được rỗng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If

    End Sub

    Private Sub bindingNavigatorDeleteItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorDeleteItem.Click
        If DGVKhachHang.RowCount = 0 Then
            bindingNavigatorDeleteItem.Enabled = False

        ElseIf MessageBox.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            BindingNavigatorKhachHang.BindingSource.RemoveCurrent()
        End If

    End Sub

    Private Sub bindingNavigatorCloseItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorCloseItem.Click
        Me.Close()
    End Sub

    Private Sub bindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorAddNewItem.Click
        If DGVKhachHang.RowCount = 0 Then
            BindingNavigatorKhachHang.Enabled = True
        End If
        Dim m_Row As DataRow = m_KhachHangCtrl.ThemDongMoi()
        m_Row("MaKH") = "KH" & m_QuyDinh.LaySTT(DGVKhachHang.Rows.Count + 1)


        m_Row("HoTen") = ""
        m_Row("DiaChi") = ""
        m_Row("DienThoai") = ""
        m_KhachHangCtrl.ThemMoi(m_Row)
        BindingNavigatorKhachHang.BindingSource.MoveLast()
    End Sub
#Region "Tìm kiếm khách hàng"

    Private Sub btnSearch_KeyDown(sender As Object, e As KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            TimKiemKhachHang()
        End If
    End Sub

    Private Sub TimKiemKhachHang()
        Try
            If cbMaKH.Checked = True Then
                m_KhachHangCtrl.TimKiemTheoMa(txtTimKiem.Text)
            Else
                m_KhachHangCtrl.TimKiemTheoTen(txtTimKiem.Text)

            End If
        Catch
            MessageBoxEx.Show("Không thể tìm thấy Giá trị cần tìm!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub btnTimKiem_Click(sender As System.Object, e As System.EventArgs) Handles btnTimKiem.Click
        If txtTimKiem.Text = "" Then
            MessageBoxEx.Show("Giá trị tìm rỗng!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        Else
            TimKiemKhachHang()
        End If

    End Sub
#End Region

    Private Sub buttonSave_Click(sender As System.Object, e As System.EventArgs) Handles buttonSave.Click
        If KiemTraTruocKhiLuu("colMaKH") = True AndAlso KiemTraTruocKhiLuu("colDiaChi") = True AndAlso KiemTraTruocKhiLuu("colHoTen") = True Then
            bindingNavigatorPositionItem.Focus()
            m_KhachHangCtrl.LuuKhachHang()
            MessageBoxEx.Show("Đã lưu thành công!", "Hoàn Thành", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBoxEx.Show("Giá trị ô không được rỗng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If

    End Sub

    Private Sub btnHuy_Click(sender As System.Object, e As System.EventArgs) Handles btnHuy.Click
        Me.Close()
    End Sub
End Class